package demo;

public class HaversineDemo {

    public static void main(String[] args) {
        // 经纬度坐标1
        double lon1 = 118.91032527073807;
        double lat1 = 32.115069846696386;

        // 经纬度坐标2
        double lon2 =118.91099197425791;
        double lat2 =32.11787259170835;

        // 使用 Haversine 公式计算距离
        double distance = haversine(lat1, lon1, lat2, lon2);

        System.out.println("真实距离: " + distance + " 米");
    }

    private static double haversine(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return 6371e3 * c; // 6371e3 是地球半径（单位是米）
    }
}
